package com.tencent.cloud.iov.flow.view.impl;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;

import com.tencent.cloud.iov.action.ActionManager;
import com.tencent.cloud.iov.action.IActionListener;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.flow.presenter.IPresenter;
import com.tencent.cloud.iov.flow.view.IFlowView;

/**
 * 视图逻辑抽象类
 *
 * 视图创建时进行 V - P 绑定，销毁时进行 V - P 解绑
 * 视图前台可见时通知 P 触发加载数据，视图前台不可见时通知 P 结束数据加载
 *
 * 注意：
 * 设置视图时，需要使用当前类的 setContentView() 方法，以兼容当前视图在非 Activity 容器下的使用，
 * 避免使用 {@link Activity#setContentView} 进行视图设置
 *
 * @author showxiao
 * @date 2018/5/3
 */

public abstract class BaseFlowView<P extends IPresenter>
        implements IFlowView, IContentViewContainer, IActionListener {

    private final Activity mActivity;
    private final IContentViewContainer mFlowViewContainer;
    private P mPresenter;

    public BaseFlowView(Activity activity) {
        this(activity, null);
    }

    public BaseFlowView(Activity activity, IContentViewContainer container) {
        mActivity = activity;
        mFlowViewContainer = container;
    }

    @CallSuper
    @Override
    public void onCreate() {
        preparePresenter();
    }

    @CallSuper
    @Override
    public void onStart() {
        resumePresenter();
    }

    @CallSuper
    @Override
    public void onResume() {

    }

    @CallSuper
    @Override
    public void onPause() {

    }

    @CallSuper
    @Override
    public void onStop() {
        pausePresenter();
    }

    @CallSuper
    @Override
    public void onDestroy() {
        destroyPresenter();
    }

    @Override
    public boolean onAction(String actionId, Object... args) {
        return ActionManager.handleAction(this, actionId, args);
    }

    public final Activity getActivity() {
        return mActivity;
    }

    /**
     * 创建 MVP - P
     *
     * @return  MVP - P
     */
    protected abstract P createPresenter();

    public final void setPresenter(P presenter) {
        mPresenter = presenter;
    }

    public final P getPresenter() {
        return mPresenter;
    }

    protected void preparePresenter() {
        if (mPresenter == null) {
            setPresenter(createPresenter());
        }
        if (mPresenter != null) {
            mPresenter.onCreate();
            mPresenter.setActionListener(this);
            mPresenter.startLoader();
        }
    }

    protected void resumePresenter() {
        if (mPresenter != null) {
            mPresenter.registerLoader();
        }
    }

    protected void pausePresenter() {
        if (mPresenter != null) {
            mPresenter.unregisterLoader();
        }
    }

    protected void destroyPresenter() {
        if (mPresenter != null) {
            mPresenter.stopLoader();
            mPresenter.setActionListener(null);
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }

    /**
     * 设置布局
     *
     * @param resId
     */
    @Override
    public void setContentView(@LayoutRes int resId) {
        if (mFlowViewContainer != null) {
            mFlowViewContainer.setContentView(resId);
        } else {
            mActivity.setContentView(resId);
        }
    }

    /**
     * 设置布局
     *
     * @param contentView
     */
    @Override
    public void setContentView(View contentView) {
        if (mFlowViewContainer != null) {
            mFlowViewContainer.setContentView(contentView);
        } else {
            mActivity.setContentView(contentView);
        }
    }

    /**
     * 设置布局
     *
     * @param contentView
     * @param params
     */
    @Override
    public void setContentView(View contentView, ViewGroup.LayoutParams params) {
        if (mFlowViewContainer != null) {
            mFlowViewContainer.setContentView(contentView, params);
        } else {
            mActivity.setContentView(contentView, params);
        }
    }

    @Override
    public final Context getContext() {
        return mActivity != null ? mActivity :
                (mFlowViewContainer != null ? mFlowViewContainer.getContext() : null);
    }
}
